package com.android.hedgehog;

import java.util.ArrayList;

import android.graphics.Color;

import com.android.hedgehog.framework.Graphics;

public class Keyboard {
	public Letter[] letters;
	public Letter[] numbers;
	String LETTERS = "QWERTYUIOPASDFGHJKLZXCVBNM-";
	String NUMBERS = "1234567890";
	int inputSizeLimit = 15;
	final int keyWidth = 27;
	final int keyHeight = 38;
	int delYpos;
	public boolean lettersOn;
	public boolean numbersOn;
	ArrayList<Letter> input = new ArrayList<Letter>();
	public String finalResult;

	public Keyboard(boolean lettersOn, boolean numbersOn) {
		setMode(lettersOn, numbersOn);
		setKeyboard();

	}

	public void setMode(boolean lettersOn, boolean numbersOn) {
		this.lettersOn = lettersOn;
		this.numbersOn = numbersOn;
		input.clear();
		finalResult = "";
		if (!lettersOn) {
			delYpos = 300;
		} else {
			delYpos = 390;
		}
	}

	private void setKeyboard() {

		letters = new Letter[LETTERS.length()];

		for (int i = 0; i < 10; i++) {
			letters[i] = new Letter(LETTERS.charAt(i));
			letters[i].setPosition(3 + (i * 32), 300, keyWidth, keyHeight);
		}
		for (int i = 0; i < 9; i++) {
			letters[i + 10] = new Letter(LETTERS.charAt(i + 10));
			letters[i + 10].setPosition(15 + (i * 32), 345, keyWidth, keyHeight);
		}
		for (int i = 0; i < 8; i++) {
			letters[i + 19] = new Letter(LETTERS.charAt(i + 19));
			letters[i + 19].setPosition(28 + (i * 32), 390, keyWidth, keyHeight);
		}

		numbers = new Letter[NUMBERS.length()];

		for (int i = 0; i < NUMBERS.length(); i++) {
			numbers[i] = new Letter(NUMBERS.charAt(i));
			numbers[i].setPosition(3 + (i * 32), 255, keyWidth, keyHeight);
		}

	}

	public void click(int x, int y) {

		if (input.size() < inputSizeLimit) {

			if (lettersOn) {
				for (int i = 0; i < letters.length; i++) {
					if (x > letters[i].x && x < (letters[i].x + letters[i].width) && y > letters[i].y
							&& y < (letters[i].y + letters[i].height))
						input.add(letters[i]);
				}
			}
			if (numbersOn) {
				for (int i = 0; i < numbers.length; i++) {
					if (x > numbers[i].x && x < (numbers[i].x + numbers[i].width) && y > numbers[i].y
							&& y < (numbers[i].y + numbers[i].height))
						input.add(numbers[i]);
				}
			}
		}

		if (x > 284 && y > delYpos && y < delYpos + keyHeight) {
			if (input.size() > 0) {
				input.remove(input.size() - 1);
			}
		}
		concatenate();

	}

	public void DisplayKeyboard(Graphics graphics) {

		if (lettersOn) {
			for (int i = 0; i < letters.length; i++) {
				graphics.drawUniversalRect(letters[i].x, letters[i].y, letters[i].width, letters[i].height,
						false, true, 1, 255, Color.GRAY);
				graphics.drawUniversalRect(letters[i].x, letters[i].y, letters[i].width, letters[i].height,
						false, false, 1, 255, Color.DKGRAY);
				graphics.drawUniversalText("" + letters[i].letter, 2, true, letters[i].letterX,
						letters[i].letterY, 20, 255, Color.WHITE);
			}
		}

		if (numbersOn) {
			for (int i = 0; i < numbers.length; i++) {
				graphics.drawUniversalRect(numbers[i].x, numbers[i].y, numbers[i].width, numbers[i].height,
						false, true, 1, 255, Color.GRAY);
				graphics.drawUniversalRect(numbers[i].x, numbers[i].y, numbers[i].width, numbers[i].height,
						false, false, 1, 255, Color.DKGRAY);
				graphics.drawUniversalText("" + numbers[i].letter, 2, true, numbers[i].letterX,
						numbers[i].letterY, 20, 255, Color.WHITE);
			}
		}

		graphics.drawUniversalRect(284, delYpos, keyWidth + 9, keyHeight, false, true, 1, 255, Color.GRAY);
		graphics.drawUniversalRect(284, delYpos, keyWidth + 9, keyHeight, false, false, 2, 255, Color.DKGRAY);
		graphics.drawUniversalText("DEL", 2, true, 302, delYpos + 27, 16, 255, Color.RED);

	}

	public void concatenate() {
		finalResult = "";
		for (int i = 0; i < input.size(); i++) {
			finalResult += input.get(i).letter;
		}
	}

}
